package com.wwj.core.pay.weixin.transfer;

import com.wwj.core.pay.weixin.Configure;
import com.wwj.core.pay.weixin.XMLParser;
import com.wwj.core.pay.weixin.refund.ClientCustomSSL;
import com.wwj.core.pay.weixin.refund.RequestHandler;
import com.wwj.utils.IpUtil;
import com.wwj.utils.StringUtil;
import java.math.BigDecimal;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by Auser on 2017/1/11.
 */
public class TransferUtil {

    public static Map<String,Object> wechatTransfer(String partner_trade_no,String openId,BigDecimal transferAmont,String re_user_name,String desc){
        String ip = IpUtil.getMyIP();
        int amount = transferAmont.intValue();// 退款金额，以分为单位（填错了貌似提示：同一个out_refund_no退款金额要一致,默认单位分）
        String nonce_str = StringUtil.getRandomStringByLength(32);;// 随机字符串
        //微信公众平台文档：“基本配置”--》“开发者ID”
        String appid = Configure.appID;
        //微信公众平台文档：“基本配置”--》“开发者ID”
        String appsecret = Configure.getAppSecret();
        //商户号
        //微信公众平台文档：“微信支付”--》“商户信息”--》“商户号”，将该值赋值给partner
        String mch_id = Configure.mchID;
        //微信公众平台："微信支付"--》“商户信息”--》“微信支付商户平台”（登录）--》“API安全”--》“API密钥”--“设置密钥”（设置之后的那个值就是partnerkey，32位）
        String partnerkey = Configure.getKey();
        SortedMap<String, String> packageParams = new TreeMap<String, String>();
        packageParams.put("mch_appid", appid);
        packageParams.put("mchid", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("partner_trade_no", partner_trade_no);
        packageParams.put("openid", openId);
        packageParams.put("check_name", "OPTION_CHECK");
        packageParams.put("re_user_name", re_user_name);
        packageParams.put("amount", amount + "");
        packageParams.put("desc", desc);
        packageParams.put("spbill_create_ip", ip);


        RequestHandler reqHandler = new RequestHandler(null, null);
        reqHandler.init(appid, appsecret, partnerkey);
        String sign = reqHandler.createSign(packageParams);
        String xml = "<xml>" +
                        "<mch_appid>" + appid + "</mch_appid>" +
                        "<mchid>" + mch_id + "</mchid>" +
                        "<nonce_str>" + nonce_str + "</nonce_str>" +
                        "<partner_trade_no>" + partner_trade_no + "</partner_trade_no>" +
                        "<openid>" + openId + "</openid>" +
                        "<check_name>OPTION_CHECK</check_name>" +
                        "<re_user_name>" + re_user_name + "</re_user_name>" +
                        "<amount>" + amount + "</amount>" +
                        "<desc>" + desc + "</desc>" +
                        "<spbill_create_ip>" + ip + "</spbill_create_ip>" +
                        "<sign><![CDATA[" + sign + "]]></sign>"	+
                    "</xml>";
        String createOrderURL = Configure.TRANSFAER_API;
        Map<String, Object> mapFromXML = null;
        try {
            String transferResult= ClientCustomSSL.doSend(createOrderURL, xml);
            mapFromXML = XMLParser.getMapFromXML(transferResult);
            System.out.println("转账产生的json字符串："+ mapFromXML);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapFromXML;
    }

    public static void main(String[] args) {
//        wechatTransfer("123123123123123","ojoREwMarN03GI-tQJTLKaJaQnic",new BigDecimal(1),"唐运","转账");
        String localIP = IpUtil.getMyIP();
        System.out.println(localIP);
    }

}
